//
//  AuthenCompanyMarketPhotoVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import MCToast

class AuthenCompanyMarketPhotoVM: UIView {
    
    var selfHeight = AuthenPhotoVM().selfHeight*2+RegisterViewHeadTitleVM().selfHeight
    
    var controller = WHBaseViewVC()
    var imgSourceType = PHOTOTYPE.mentou
    var imgType = ""
    
    override init(frame:CGRect){
        if UserModel.shared.posChannel == "ys" || UserModel.shared.posChannel == "fy"{
            selfHeight = AuthenPhotoVM().selfHeight*3+RegisterViewHeadTitleVM().selfHeight
        }
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        //authen_photo_id_front
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var photoTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "门店照"
        
        return vm
    }()
    lazy var menTouPhotoVm : AuthenPhotoVM = {
        let vm = AuthenPhotoVM.init(frame: CGRect.init(x: 0, y: self.photoTitleVm.frame.maxY, width: 0, height: 0  ))
        vm.titleLabel.text = "门头照"
        vm.photoBlock = {()in
            self.imgSourceType = .mentou
            self.imgType = "marketMenTou"
            self.takePickture()
        }
        vm.exampleBlock = {()in
            self.sampleAlertVm.showExampleImage(type: .mentouCompany)
        }
        return vm
    }()
    lazy var dianNeiPhotoVm : AuthenPhotoVM = {
        let vm = AuthenPhotoVM.init(frame: CGRect.init(x: SCREEN_WIDHT*0.5, y: self.photoTitleVm.frame.maxY, width: 0, height: 0  ))
        vm.titleLabel.text = "店内环境照"
        vm.photoBlock = {()in
            self.imgSourceType = .diannei
            self.imgType = "marketDianNei"
            self.takePickture()
        }
        vm.exampleBlock = {()in
            self.sampleAlertVm.showExampleImage(type: .dianneiCompany)
        }
        return vm
    }()
    lazy var cashPhotoVm : AuthenPhotoVM = {
        let vm = AuthenPhotoVM.init(frame: CGRect.init(x: 0, y: self.menTouPhotoVm.frame.maxY, width: 0, height: 0  ))
        vm.titleLabel.text = "收银台照"
        vm.photoBlock = {()in
            self.imgSourceType = .cash
            self.imgType = "marketCash"
            self.takePickture()
        }
        vm.exampleBlock = {()in
            self.sampleAlertVm.showExampleImage(type: .cashCompany)
        }
        return vm
    }()
    lazy var dianWaiPhotoVm : AuthenPhotoVM = {
        let vm = AuthenPhotoVM.init(frame: CGRect.init(x: SCREEN_WIDHT*0.5, y: self.menTouPhotoVm.frame.maxY, width: 0, height: 0  ))
        vm.titleLabel.text = "门店照（店外环境照）"
        vm.photoBlock = {()in
            self.imgSourceType = .dianwai
            self.imgType = "marketDianNei"
            self.takePickture()
        }
        vm.exampleBlock = {()in
            self.sampleAlertVm.showExampleImage(type: .dianwaiCompany)
        }
        return vm
    }()
    
    lazy var mercLocationPhotoVm : AuthenPhotoVM = {
        let vm = AuthenPhotoVM.init(frame: CGRect.init(x: 0, y: self.cashPhotoVm.frame.maxY, width: 0, height: 0  ))
        vm.titleLabel.text = "实际经营地址定位"
        vm.photoBlock = {()in
            self.imgSourceType = .location
            self.imgType = "marketLocation"
            self.takePickture()
        }
        vm.exampleBlock = {()in
            
            self.sampleAlertVm.showExampleImage(type: .location)
        }
        return vm
    }()
    lazy var sampleAlertVm : AuthenPhotoSampleAlertVM = {
        let vm = AuthenPhotoSampleAlertVM.init(frame: .zero)
        vm.isHidden = true
        return vm
    }()
}

extension AuthenCompanyMarketPhotoVM{
    func initUI() {
        addSubview(photoTitleVm)
        addSubview(menTouPhotoVm)
        addSubview(dianNeiPhotoVm)
        addSubview(cashPhotoVm)
        addSubview(dianWaiPhotoVm)
        if UserModel.shared.posChannel == "ys"{
            addSubview(mercLocationPhotoVm)
        }else if UserModel.shared.posChannel == "fy"{
            addSubview(mercLocationPhotoVm)
            mercLocationPhotoVm.titleLabel.text = "其他图片"
            mercLocationPhotoVm.exampleImgView.isHidden = true
            mercLocationPhotoVm.exampleLabel.isHidden = true
            mercLocationPhotoVm.exampleTapView.isHidden = true
        }
        
        UIApplication.shared.keyWindow?.addSubview(sampleAlertVm)
    }
    func updateUIForRefuse() {
        menTouPhotoVm.setImgUrl(urlstring: "\(AuthenCompanyMsgModel.shareInstance.photoImgUrlMenTou)")
        dianNeiPhotoVm.setImgUrl(urlstring: "\(AuthenCompanyMsgModel.shareInstance.photoImgUrlDianNei)")
        cashPhotoVm.setImgUrl(urlstring: "\(AuthenCompanyMsgModel.shareInstance.photoImgUrlCash)")
        dianWaiPhotoVm.setImgUrl(urlstring: "\(AuthenCompanyMsgModel.shareInstance.photoImgUrlDianWai)")
        mercLocationPhotoVm.setImgUrl(urlstring: "\(AuthenCompanyMsgModel.shareInstance.photoImgUrlLocation)")
    }
    func setReadMode() {
        menTouPhotoVm.setReadMode()
        dianNeiPhotoVm.setReadMode()
        cashPhotoVm.setReadMode()
        dianWaiPhotoVm.setReadMode()
        mercLocationPhotoVm.setReadMode()
    }
}

extension AuthenCompanyMarketPhotoVM:UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    @objc func takePickture(){
        let alertController=UIAlertController(title: "请选择照片来源", message: nil, preferredStyle: .actionSheet)

        let cancel=UIAlertAction(title:"取消", style: .cancel, handler: nil)
        
        let takingPictures=UIAlertAction(title:"拍照", style: .default){ action in
            if UIImagePickerController.isSourceTypeAvailable(.camera){
                let  cameraPicker = UIImagePickerController()
                cameraPicker.delegate = self
                cameraPicker.sourceType = .camera
                //在需要的地方present出来
                self.controller.present(cameraPicker, animated: true, completion: nil)
            } else {
                print("不支持拍照")
            }
        }
        let photoPictures=UIAlertAction(title:"相册", style: .default){ action in
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
                let  cameraPicker = UIImagePickerController()
                cameraPicker.delegate = self
                cameraPicker.allowsEditing = true
                cameraPicker.sourceType = .photoLibrary
                //在需要的地方present出来
                self.controller.present(cameraPicker, animated: true, completion: nil)
            } else {
                print("不支持拍照")
            }
        }
        
        alertController.addAction(cancel)
        alertController.addAction(takingPictures)
        alertController.addAction(photoPictures)
        
        self.controller.present(alertController, animated:true, completion:nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        print("获得照片============= \(info)")
        self.controller.dismiss(animated: true, completion: nil)
        MCToast.mc_loading()
        let image = (info[UIImagePickerController.InfoKey.originalImage] as? UIImage)!
        let imageData = WHRequestTools.compressImage(toData: image)
//        let imageData = image.jpegData(compressionQuality: 1.0)
        
        DSImageUploader.shared.uploadImages(_image: imageData!,imgType: "\(self.imgType)") { text, value in
            DLLog(message: "\(text)")
            DLLog(message: "\(value)")
            MCToast.mc_remove()
            if value == true{
                if self.imgSourceType == .mentou{
                    self.menTouPhotoVm.setImgUrl(urlstring: "\(text)")
                    AuthenCompanyMsgModel.shareInstance.photoImgUrlMenTou = "\(text)"
                }else if self.imgSourceType == .diannei{
                    self.dianNeiPhotoVm.setImgUrl(urlstring: "\(text)")
                    AuthenCompanyMsgModel.shareInstance.photoImgUrlDianNei = "\(text)"
                }else if self.imgSourceType == .cash{
                    self.cashPhotoVm.setImgUrl(urlstring: "\(text)")
                    AuthenCompanyMsgModel.shareInstance.photoImgUrlCash = "\(text)"
                }else if self.imgSourceType == .dianwai{
                    self.dianWaiPhotoVm.setImgUrl(urlstring: "\(text)")
                    AuthenCompanyMsgModel.shareInstance.photoImgUrlDianWai = "\(text)"
                }else if self.imgSourceType == .location{
                    self.mercLocationPhotoVm.setImgUrl(urlstring: "\(text)")
                    AuthenCompanyMsgModel.shareInstance.photoImgUrlLocation = "\(text)"
                }
            }else{
                MCToast.mc_text("图片上传失败")
            }
        }
    }
}

